﻿/*
 * Copyright 2011 Hanns Kucer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
using System;
using System.IO;
using System.Text;
using System.Net.Sockets;

namespace iTachToolkit
{
    public class iTach
    {
        protected iTach()
        {
        }

        public virtual void Send(string data)
        {
            throw new NotImplementedException();
        }

        public virtual string Receive()
        {
            throw new NotImplementedException();
        }

        public virtual string Trancieve(string data)
        {
            Send(data);
            return Receive();
        }

        public virtual bool IsConnected
        {
            get
            {
                return true;
            }
        }

        public virtual void Close()
        {
        }

        public virtual string Version
        {
            get
            {
                return Trancieve("getversion");
            }
        }

        ~iTach()
        {
			try
			{
				Close();
			}
			catch
			{
				// do not throw in destructor
			}
        }

        public class VirtualiTach : iTach
        {
            private string data = "";

            public VirtualiTach()
                : base()
            {
            }

            public override void Send(string data)
            {
                this.data = data.Trim();
            }

            public override string Receive()
            {
                string res;

				if (data.StartsWith("getversion"))
					res = GetType().Name;
				else if (data.StartsWith("sendir,"))
				{
					var p = data.Split(',');
					res = "completeir," + p[1] + "," + p[2];
				}
				else if (data.StartsWith("stopir"))
					res = data;
				else
					res = "ERR_0:0,001"; // unknown command

                data = "";
                return res;
            }
        }

        public class IpiTach : iTach
        {
            private Tube tube;

            public IpiTach(EndPoint.IpEndPoint ep)
            {
                tube = TubeDispatcher.iTachTube(ep.Host, ep.Port);
            }

            public override bool IsConnected
            {
                get
                {
                    return tube != null && tube.IsConnected;
                }
            }

            public override void Send(string data)
            {
                tube.Send(data);
            }

            public override string Receive()
            {
                return tube.Receive();
            }

            public override void Close()
            {
                if(tube != null && tube.IsConnected)
                    tube.Disconnect();
            }
        }
    }
}